﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace ado_test
{
    public partial class LoginForm : Form
    {
        OleDbConnection conn = null;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            //SqlConnection conn = new SqlConnection();
            //conn.ConnectionString = "server=.;database=test;integrated security=true;";

            conn = new OleDbConnection();
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\..\\student.accdb";

            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                //MessageBox.Show("已连接!");
                this.Text = "已连接!";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((this.txtUsername.Text == "") || (this.txtPassword.Text == ""))
            {
                MessageBox.Show("用户名或密码不能为空!");
                return;
            }

            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = this.conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select password from users where username='" + this.txtUsername.Text + "'";
            string pwd = (string)cmd.ExecuteScalar();
            if (pwd == null)
            {
                MessageBox.Show("用户名不存在，请重试!");
            }
            else if (pwd != this.txtPassword.Text)
            {
                MessageBox.Show("密码错误，请重试!");
            }
            else
            {
                //MessageBox.Show("登录成功!");
                MainForm f = new MainForm(this, this.txtUsername.Text);
                //f.loginform = this;
                this.Hide();
                f.Show();
            }
        }
    }
}
